package com.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.ResponseResult;
import com.demo.domain.entity.LogisticsArea;
import com.demo.domain.entity.LogisticsSend;
import com.demo.domain.entity.PersonalInformation;
import com.demo.domain.vo.AreaVo;
import com.demo.domain.vo.RouterAreaVo;
import com.demo.mapper.LogisticsSendMapper;
import com.demo.mapper.PersonalInformationMapper;
import com.demo.service.LogisticsAreaService;
import com.demo.mapper.LogisticsAreaMapper;
import com.demo.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
*
*/
@Service
public class LogisticsAreaServiceImpl extends ServiceImpl<LogisticsAreaMapper, LogisticsArea>
implements LogisticsAreaService{
    @Autowired
    private PersonalInformationMapper personalInformationMapper;
    @Autowired
    private LogisticsAreaMapper logisticsAreaMapper;
    @Autowired
    private LogisticsSendMapper logisticsSendMapper;
    @Override
    public ResponseResult nextstation(Integer orderNumber) {
        LambdaQueryWrapper<LogisticsSend> queryWrapper2=new LambdaQueryWrapper<>();
        queryWrapper2.eq(LogisticsSend::getOrderNumber,orderNumber);
        LogisticsSend logisticsSend=logisticsSendMapper.selectOne(queryWrapper2);

        if (!Objects.equals(logisticsSend.getOrderStatus(), "出库") && !Objects.equals(logisticsSend.getOrderStatus(), "已支付")){
            return ResponseResult.errorResult(104,"该订单未出库或者未支付，不能入库");
        }
        LambdaQueryWrapper<PersonalInformation> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(PersonalInformation::getAccount, SecurityUtils.getLoginUser().getUsername());
        PersonalInformation personalInformation= personalInformationMapper.selectOne(queryWrapper);
        LambdaQueryWrapper<LogisticsArea> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(LogisticsArea::getLng,personalInformation.getLng());
        queryWrapper1.eq(LogisticsArea::getLat,personalInformation.getLat());
        queryWrapper1.eq(LogisticsArea::getOrderNumber,orderNumber);
         int s =logisticsAreaMapper.selectCount(queryWrapper1);
         if (s>0){
             return ResponseResult.errorResult(103,"物流已经在此处入库");
         }else {
             UpdateWrapper<LogisticsSend> updateWrapper=new UpdateWrapper<>();
             updateWrapper.lambda().set(LogisticsSend::getOrderStatus,"入库")
                     .eq(LogisticsSend::getOrderNumber,logisticsSend.getOrderNumber());
             logisticsSendMapper.update(logisticsSend,updateWrapper);
             LogisticsArea logisticsArea=new LogisticsArea();
             logisticsArea.setOrderNumber(orderNumber.toString());
             logisticsArea.setRouteArea(personalInformation.getArea());
             logisticsArea.setLng(personalInformation.getLng());
             logisticsArea.setLat(personalInformation.getLat());
             try {
                 Date date = new Date();
                 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                 Date date1 = dateFormat.parse(dateFormat.format(date));
                 logisticsArea.setRouteTime(date1);
             } catch (Exception e) {

             }

             return ResponseResult.okResult(save(logisticsArea));
         }

    }

    @Override
    public ResponseResult LogisticsMap(Integer orderNumber) {
        LambdaQueryWrapper<LogisticsArea> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(LogisticsArea::getOrderNumber,orderNumber);
        queryWrapper.orderByAsc(LogisticsArea::getRouteTime);
        List<LogisticsArea> list=list(queryWrapper);
        List<AreaVo> arrayList=new ArrayList<>();


        LambdaQueryWrapper<LogisticsSend> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(LogisticsSend::getOrderNumber,orderNumber);
        LogisticsSend logisticsSend=logisticsSendMapper.selectOne(queryWrapper1);
        AreaVo areaVo1=new AreaVo();
        areaVo1.setLng(logisticsSend.getSendLng());
        areaVo1.setLat(logisticsSend.getSendLat());
        arrayList.add(areaVo1);
        for (LogisticsArea logisticsArea : list) {
            AreaVo areaVo = new AreaVo();
            areaVo.setLng(logisticsArea.getLng());
            areaVo.setLat(logisticsArea.getLat());
            arrayList.add(areaVo);
        }
        AreaVo areaVo2=new AreaVo();
        areaVo2.setLng(logisticsSend.getRecipientLng());
        areaVo2.setLat(logisticsSend.getRecipientLat());
        arrayList.add(areaVo2);
        RouterAreaVo routerAreaVo=new RouterAreaVo();
        routerAreaVo.setArrayList(arrayList);
        if (Objects.equals(logisticsSend.getOrderStatus(), "送达")){
            routerAreaVo.setState(0);
        }else {
            routerAreaVo.setState(1);
        }
        return ResponseResult.okResult(routerAreaVo);
    }
}
